package com.zhou.cloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.zhou.cloud.dao.entity.Dept;
import com.zhou.cloud.service.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhouyongjun
 * @Description:
 * @date 2021/4/6 16:45
 */
@RestController
@RequestMapping("dept")
@Slf4j
public class DeptController {
    @Autowired
    IDeptService deptService;
    @GetMapping("/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        Dept dept = deptService.get(id);
        System.out.println(dept);
        return dept;

    }
    @PostMapping("/add")
    public boolean add(Dept dept) {
        return deptService.add(dept);
    }

    @DeleteMapping("del/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return false;
    }

    @GetMapping("/list")
    // SentinelResource 注解 https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81
    @SentinelResource(value = "provider-list",fallback = "listFallback")
    public List<Dept> list() {
        List<Dept> list = deptService.getAll();
        System.out.println(list);
        return list;
    }
    @GetMapping("test")
//    fallback = "testBlockHandler",
    @SentinelResource(value = "test",blockHandler = "testBlockHandler",defaultFallback =  "testFallback")
    public String test() {
        String s = "33fdd";
        int d = Integer.parseInt(s);
       return "test";
    }
    public String testFallback()
    {
        return "testFallback..... ";
    }
    public String testBlockHandler()
    {
        return "testBlockHandler..... ";
    }


    public List<Dept> listFallback(){
        String msg = "listfallback ...";
        log.warn("<<<<fallback");
        return null;
    }

    public List<Dept> listFlowException(FlowException e) {
//        return "cousumer 限流了！";
        log.warn("<<<<限流了！");
        return null;
    }

    // 自定义异常必须与原方法返回值及参数完全一致
    public List<Dept> degradeException(BlockException e) {
//        return " cousumer 降级了！";
        log.warn("<<<<降级了！");
        return null;
    }
}
